package com.example.kangjujiaofei.utils;

import com.alibaba.fastjson.JSON;
import com.example.kangjujiaofei.entity.XSCReponseDataResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

//
@Component
@Slf4j
public class TokenViewUtil {

    final static String XIN_SHUACHANG_API = "http://192.168.118.35:8009/newseeserver/newseeserver.aspx";


    @Resource
    private AesEncryptUtils aesEncryptUtils;

    //获取tokenview公链信息
    //区号加手机号的phone
    public String getInfo(String url) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        String back = response.getBody();
        return back;
    }

    public String getXinShiChuanData(Integer NWCode, String NWGUID, String NWExID, Map<String, Object> data1) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "text/xml");
        Map<String, Map<String, Object>> data = new HashMap<>();
        //头
        Map<String, Object> head = new HashMap<>();
        head.put("NWVersion", 1);
        head.put("NWCode", NWCode);
        head.put("NWGUID", NWGUID);
        head.put("NWExID", NWExID);
        //存入头
        data.put("Head", head);
        data.put("Data", data1);

        Map<String, Object> endData = new HashMap<>();
        endData.put("Request", data);
        String dataJson = JSON.toJSONString(endData);
        String requestData = "";
        try {
            requestData = aesEncryptUtils.encrypt(dataJson);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }

        HttpEntity<String> request = new HttpEntity<String>(requestData, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(XIN_SHUACHANG_API, request, String.class);
        String result = null;
        try {
            result = aesEncryptUtils.decrypt(response.getBody());
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public String getXinShiChuanLogin(String NWGUID) throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "text/xml");
        Map<String, Map<String, Object>> data = new HashMap<>();
        //头
        Map<String, Object> head = new HashMap<>();
        head.put("NWVersion", 1);
        head.put("NWCode", 1);
        head.put("NWGUID", NWGUID);
        //存入头
        data.put("Head", head);
        Map<String, Object> loginAdmin = new HashMap<>();
        loginAdmin.put("Account", "梅洋洋");
        loginAdmin.put("PassWord", "123456ABc");
        //放入数据
        data.put("Data", loginAdmin);
        Map<String, Object> endData = new HashMap<>();
        endData.put("Request", data);
        String dataJson = JSON.toJSONString(endData);
        String requestData  = aesEncryptUtils.encrypt(dataJson);
        HttpEntity<String> request = new HttpEntity<String>(requestData, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(XIN_SHUACHANG_API, request, String.class);
        String result = aesEncryptUtils.decrypt(response.getBody());
        XSCReponseDataResponse response1 = JSON.parseObject(result, XSCReponseDataResponse.class);
        return response1.getResponse().getHead().getNWExID();
    }

    public String httpPost(String data,String url){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "text/xml");

        HttpEntity<String> request = new HttpEntity<String>(data, httpHeaders);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
        return  response.getBody();
    }
}
